package com.shitou.springai1.user.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.providers.PooledConnectionProvider;
import redis.clients.jedis.search.RediSearchCommands;

@Configuration
public class RedisConfig {

    @Value("${spring.data.redis.host}")
    private String redisHost;

    @Value("${spring.data.redis.port}")
    private int redisPort;

    @Value("${spring.data.redis.password:}")
    private String redisPassword;

    @Value("${spring.data.redis.database:0}")
    private int database;

    @Value("${app.redis.index-name}")
    private String indexName;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
        if (redisPassword != null && !redisPassword.isEmpty()) {
            config.setPassword(redisPassword);
        }
        config.setDatabase(database);
        return new JedisConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(10);
        poolConfig.setMaxIdle(5);
        poolConfig.setMinIdle(1);
        poolConfig.setJmxEnabled(false);
        return poolConfig;
    }

/**
 * 创建并配置JedisPool Bean，用于管理Redis连接池
 *
 * @param poolConfig Jedis连接池配置对象，包含池大小、最大空闲连接等参数
 * @return 返回配置好的JedisPool实例
 */
    @Bean
    public JedisPool jedisPool(JedisPoolConfig poolConfig) {
    // 根据Redis密码是否存在来决定是否使用认证连接
        if (redisPassword != null && !redisPassword.isEmpty()) {
        // 使用密码认证方式创建Redis连接池
            return new JedisPool(poolConfig, redisHost, redisPort, 2000, redisPassword, database);
        } else {
        // 不使用密码认证方式创建Redis连接池
            return new JedisPool(poolConfig, redisHost, redisPort, 2000, null, database);
        }
    }

/**
 * 创建并配置 RediSearchCommands Bean 的方法
 * 该方法用于配置 Redis 连接并返回支持 RediSearch 的命令对象
 *
 * @param poolConfig Jedis 连接池配置对象
 * @return 配置好的 RediSearchCommands 实例
 */
    @Bean
    public RediSearchCommands rediSearchCommands(JedisPoolConfig poolConfig) {
        // 创建 HostAndPort 对象，指定 Redis 服务器的主机和端口
        HostAndPort hostAndPort = new HostAndPort(redisHost, redisPort);

        // 创建连接提供者，用于管理 Redis 连接池
        PooledConnectionProvider connectionProvider;
    // 判断是否设置了 Redis 密码
        if (redisPassword != null && !redisPassword.isEmpty()) {
        // 如果设置了密码，创建需要认证的连接提供者
            connectionProvider = new PooledConnectionProvider(
                    hostAndPort
            );
        } else {
        // 如果没有设置密码，创建普通连接提供者
            connectionProvider = new PooledConnectionProvider(
                    hostAndPort
            );
        }

        // 创建 JedisPooled 实例，用于执行 Redis 命令
        JedisPooled jedisPooled = new JedisPooled(connectionProvider);

    // 返回配置好的 RediSearchCommands 实例
        return jedisPooled;
    }
}
